package com.vito.oa.act.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;

import com.alibaba.dubbo.config.annotation.Service;
import com.vito.base.authoriza.dao.BaseSqlMapper;
import com.vito.base.authoriza.dao.SQLAdapterMapper;
import com.vito.base.authoriza.model.BaseSql;
import com.vito.oa.act.constant.OaActContants;
import com.vito.oa.act.dao.OaActStatisticsMapper;
import com.vito.oa.act.service.OaActStatisticsService;
import com.vito.oa.act.statistics.AxisTypeEnum;
import com.vito.oa.act.statistics.OaActStatisticsModel;
import com.vito.oa.act.statistics.ProcStatsEnum;
@Service
public class OaActStatisticsServiceImpl implements OaActStatisticsService {
	@Autowired
	private OaActStatisticsMapper oaActStatisticsMapper;
	
	@Autowired
	private SQLAdapterMapper sqlAdapterMapper;
	
	@Autowired
	private BaseSqlMapper baseSqlMapper;
	
	
	@Override
	public List<Map<String, String>> queryDeptsOfUser(String deptCode) {
		return oaActStatisticsMapper.queryDeptsOfUser(deptCode);
	}
	
	@Override
	@Cacheable(value=OaActContants.CACHE_ACT_VALUE,key="#root.method.name+'_'+#p0+'_'+#p1+'_'+#p2+'_'+#p3")
	public OaActStatisticsModel queryProcStatistics(String statsUserId,String statsDeptId,String procKey,String xTypeStr){
		OaActStatisticsModel statsModel = null;
		ProcStatsEnum procEnum = ProcStatsEnum.keyOf(procKey);
		if(procEnum != null){
			statsModel = new OaActStatisticsModel();
			statsModel.setStatsUserId(statsUserId);
			statsModel.setStatsDeptId(statsDeptId);
			statsModel.setProcKey(procKey);
			statsModel.setProcName(procEnum.getProcName());
			statsModel.setStatsTable(procEnum.getTable());
			statsModel.setStatsPkCol(procEnum.getPkCol());
			statsModel.setStatsCol(procEnum.getCol());
			statsModel.setyTypeName(procEnum.getColName());
			
			AxisTypeEnum xEnum = AxisTypeEnum.typeOf(xTypeStr);
			if(xEnum == null){
				xEnum = AxisTypeEnum.DAY;
			}
			statsModel.setProcStartTime(xEnum.getStartTime());
			statsModel.setTypeFormat(xEnum.getTypeFormat());
			statsModel.setxTypeName(xEnum.getNameCN());
			List<String> xData =xEnum.getxData();
			statsModel.setxData(xData);
			List<Map<String,Object>> list = null;
			if(statsModel.getStatsTable() == null){
				list = oaActStatisticsMapper.queryApproveCount(statsModel);
			}else{
				list = oaActStatisticsMapper.queryApproveDataSum(statsModel);
			}
			int index;
			List<Number> yData = statsModel.getyData();
			for(Map<String,Object> map : list){
				index = xData.indexOf(map.get("xData"));
				yData.add(index, (Number) map.get("yData"));
			}
			statsModel.setyData(yData);
		}
		return statsModel;
	}

	@Override
	public String queryProcessStatisticSqlId(String procKey) {
		BaseSql bSql = new BaseSql();
		bSql.setQueryId(oaActStatisticsMapper.queryProcessStatisticSqlId(procKey));
		BaseSql sql = baseSqlMapper.queryBaseSqlById(bSql);
		return null==sql?null:sql.getSqlContent();
	}

	@Override
	public Double queryStatisticProcessStatisticData(String procKey, String... params) {
		String sql = this.queryProcessStatisticSqlId(procKey);
		if(null == sql){
			return -1d;
		}
		Map<String,Object> sqlMap = new HashMap<String,Object>();
		sqlMap.put("sql", sql);
		for(int i = 0; i < params.length; i++ ){
			sqlMap.put("p"+i, params[i]);
		}
		List<Map<?,?>> resultList = sqlAdapterMapper.getDataBysql(sqlMap);
		if(null == resultList || resultList.isEmpty() || null == resultList.get(0)){
			return -1d;
		}
		Double time = Double.parseDouble(resultList.get(0).get("time").toString());
		return  time;
	}

	@Override
	public List<Map<String, String>> getProcessAbstractList(String processKey, String startTime, String endTime,
			String userId) {
		return oaActStatisticsMapper.getProcessAbstractList(processKey, startTime, endTime, userId);
	}

	@Override
	public List<Map<String, Object>> queryStatisticAllOfCompany(String startTime, String endTime,String parentDept) {
		ProcStatsEnum[] sysProArray = ProcStatsEnum.values();
		List<Map<String, Object>> resultList = new ArrayList<>();
		for(ProcStatsEnum pro : sysProArray){
			OaActStatisticsModel statsModel = new OaActStatisticsModel();
			String key = pro.getKey();
			statsModel.setProcKey(key);
			statsModel.setProcStartTime(startTime);
			statsModel.setProcEndTime(endTime);
			statsModel.setStatsDeptId(parentDept);
			statsModel.setStatsTable(pro.getTable());
			statsModel.setStatsPkCol(pro.getPkCol());
			statsModel.setStatsCol(pro.getCol());
			
			Map<String,Object> result = new HashMap<>();
			Integer total= oaActStatisticsMapper.queryApproveCountOfCompany(statsModel);
			result.put("processKey", key);
			result.put("processName", pro.getProcName());
			result.put("total", total);
			if(pro.getTable() != null){
				Double sum = oaActStatisticsMapper.queryApproveDataSumOfCompany(statsModel);
				result.put("dataSum", sum);
				result.put("dataUnit", pro.getColName().split("/")[1]);
			}
				resultList.add(result);
		}
		return resultList;
	}
	
}
